package com.powernode.sqlinjection;

import com.powernode.commons.util.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @Author Arrebol
 * @Date 2024/8/2 17:36
 * @Project jdbc
 * @Description：
 * 不使用批处理向数据库表中插入10000条记录，并且记录耗时
 * 在不适用批处理的情况下，总耗时10.813秒
 */
public class JdbcTestBatchingProcessing1 {
    public static void main(String[] args) {
        long begin = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DbUtils.getConnection();
            String insertSql = "insert into t_batch (id, name) VALUES (?,?)";
            ps = conn.prepareStatement(insertSql);
            int count = 0;
            for (int i = 1; i <= 10000; i++) {
                ps.setInt(1, i);
                ps.setString(2, "batch" + i);
                System.out.println("execute："+insertSql);
                count += ps.executeUpdate();
            }
            System.out.println("插入了" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.close(null, ps, conn);
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时" + (double)(end - begin) / 1000 + "秒");
    }
}
